package com.example.administrator.printoutindemo;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.example.administrator.printoutindemo.base.BaseActivity;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.ToastUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by noc on 2019/4/9.
 */

public class ShenPiyijianActivity extends BaseActivity {

    private RadioButton rb_yes, rb_no;
    private EditText et_ldyj;
    private TextView tv_spriqi, tv_submit, tv_spr;
    private String yijian = "";
    private RadioGroup rg_check;
    private int year, monthOfYear, dayOfMonth;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lingdaoyijian);
        rb_yes = findViewById(R.id.rb_yes);
        rb_no = findViewById(R.id.rb_no);
        et_ldyj = findViewById(R.id.et_ldyj);
        tv_spr = findViewById(R.id.tv_spr);
        tv_spriqi = findViewById(R.id.tv_spriqi);
        tv_submit = findViewById(R.id.tv_submit);
        rg_check = findViewById(R.id.rg_check);
        tv_spr.setText(SPUtil.getString(ShenPiyijianActivity.this, "username", ""));
        rg_check.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.rb_yes:
                        yijian = "同意";
                        break;
                    case R.id.rb_no:
                        yijian = "不同意";
                        break;
                }
            }
        });
        //获取系统时间
//        Calendar calendar= Calendar.getInstance();
//        year = calendar.get(Calendar.YEAR);
//        monthOfYear = calendar.get(Calendar.MONTH);
//        dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(Calendar.getInstance().getTime());
        Log.e("shifenmiao============", time);
        tv_spriqi.setText(time);
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!yijian.equals("") & !et_ldyj.getText().toString().equals("")) {
                    updateAuditingResult();
                } else {
                    Toast.makeText(ShenPiyijianActivity.this, "请填写必填项！", Toast.LENGTH_LONG).show();
                }

            }
        });

    }

    private void updateAuditingResult() {


        String methodName = "UpdateAuditingResult";

        SoapParams params = new SoapParams();
        params.put("deliveryId", SPUtil.getString(ShenPiyijianActivity.this, "peisongids", ""));
        params.put("auditingResult", yijian);
        params.put("feedback", et_ldyj.getText().toString());
        params.put("approval", tv_spr.getText().toString());
        params.put("auditingDate", tv_spriqi.getText().toString());


        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    int b = Integer.parseInt(result);
                    if (b == 1) {
                        ToastUtil.showMessage(ShenPiyijianActivity.this, "审批成功！");
                        rb_no.setClickable(false);
                        rb_yes.setClickable(false);
                        et_ldyj.setText("");
//                            tv_spr.setText("");
                    }
                    if (b == 0) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(ShenPiyijianActivity.this);
                        builder.setTitle("提示");
                        builder.setMessage("提交异常，请重新提交！");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });

                        builder.create().show();
                    }

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
            }
        }, getAapiUserName(), getApiPwd());
    }
}
